<%@include file="/page/tag.jsp"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<!--框架必需start-->
<script type="text/javascript" src="${ctp}/QUI/libs/js/jquery.js"></script>
<%-- <script type="text/javascript" src="${ctp}/js/qui/language/cn.js"></script> --%>
<script type="text/javascript" src="${ctp}/QUI/libs/js/language/cn.js"></script>
<script type="text/javascript" src="${ctp}/QUI/libs/js/framework.js"></script>
<link href="${ctp}/QUI/libs/css/import_basic.css" rel="stylesheet"	type="text/css" />
<link href="${ctp}/QUI/libs/skins/lightBlue/style.css" rel="stylesheet" type="text/css" id="theme" themeColor="deepBlue"/>
<!-- 表单验证start -->
<script src="${ctp}/QUI/libs/js/form/validationRule.js" type="text/javascript"></script>
<script src="${ctp}/QUI/libs/js/form/validation.js" type="text/javascript"></script>
<!-- 表单验证end -->

<!-- 树组件start -->
<script type="text/javascript" src="${ctp}/js/qui/ztree.js"></script>
<link type="text/css" rel="stylesheet" href="${ctp}/css/ztree.css"></link>
<!-- 树组件end -->

<!-- 树形下拉框start -->
<script type="text/javascript" src="${ctp}/QUI/libs/js/form/selectTree.js"></script>
<!-- 树形下拉框end -->

<!-- 日期选择框start -->
<script type="text/javascript" src="${ctp}/QUI/libs/js/form/datePicker/WdatePicker.js"></script>
<!-- 日期选择框end -->

<!--自动提示框start-->
<script type='text/javascript' src='${ctp}/QUI/libs/js/form/suggestion.js'></script>
<!--自动提示框end-->


<!--数据表格start-->
<script src="${ctp}/js/qui/quiGrid.js" type="text/javascript"></script>
<!--数据表格end-->

<!--框架必需end-->

<style>
.del_f{
	margin-left: 10px;
}

#tb_goods{
	width: 100%;
}
</style>
</head>
<body>

	<form id="ff" target="frmright" class="" method="post" action="" failAlert="验证未通过">
		<input type="hidden"  name="storageFlowId"  value="<c:out value="${erpStorageFlow.storageFlowId }"/>"/>
		<table class="tableStyle" formMode="line">
			<tr>
				<td>仓库：</td>
				<td colspan="">
					<select id="sStorage" prompt="请选择" class="validate[required]"  url="${ctp}/erpStorage/erpStorage_select_list.do"  name="erpStorage.erpStorageId" ></select>
					<input type="hidden" name="storageFlowType" value="${StateValue.OutStorage_key }"/>
				</td>
				<td>总价：</td>
				<td colspan="2">
					<div id="show_totalPrice" style="width: 100px;"><c:out value="${erpStorage.storageFlowTotalPrice }" default="0"/></div>
					<input type="hidden" id="totalPrice"  name="storageFlowTotalPrice" style="width: 90%;"  value="<c:out value="${erpStorage.storageFlowTotalPrice }" default="0"/>"/>
				</td>
			</tr>
			
			<tr>
				<td>备注：</td>
				<td colspan="4">
					<textarea rows="" cols="" name="remark" style="width: 95%;"></textarea>
					<div id="fromMore">
						
					</div>
				</td>
			</tr>
			
			
			<tr>
				<td colspan="5" >
					<div class="ali01">
							<table class="tableStyle">
								<tr>
									<td><div class="ali01"><div style="display: inline-block; float:left;">1商品：</div><div id="suggestionGoods" style="width: 80%;"  class="suggestion"  inputWidth="200"></div></div></td>
									<td><div class="ali01">单价：<input id="addGoodsPrice" type="text" class=""/></div></td>
									<td><div class="ali01">数量：<input id="addGoodsCount" type="text" class=""  value=""/></div></td>
								</tr>
								<tr>
									<td><div class="ali01">库存：<span id="kucunSpan"></span><input id="addGoodsUnit"  type="hidden" class=""/></div></td>
									<td colspan="2"><div class="ali02"><button type="button" id="addGood">添加商品</button></div></td>
								</tr>
							</table>
					</div>
				</td>
			</tr>
			
			<tr>
				<td colspan="5" >
					 <div id="maingrid"></div>
				</td>
			</tr>
			
			<tr>
				<td colspan="5">
					<input id="submitFBtn" type="button" value="提交" onclick="submitF()" />
				 	<input type="reset" value="取消" onclick="closeWin()" />
				 </td>
			</tr>
		</table>
	</form>
	<script>
		function closeWin() {
			top.Dialog.close();
		}

		function submitF() {
			  if($("#ff").validationEngine({returnIsValid: true})){
				  //清空list数据
				  $("#fromMore").html("");
				  var gd = g.records, j=0;
				  for(var i in gd){
				     $("#fromMore").append('<input name="detailList['+j+'].erpGoodInfo.erpGoodInfoId" type="hidden" value="'+gd[i].goodInfoId+'"/>');
					 $("#fromMore").append('<input name="detailList['+j+'].count" type="hidden" value="'+gd[i].count+'"/>');
					 $("#fromMore").append('<input name="detailList['+j+'].price" type="hidden" value="'+gd[i].cost+'"/>');
					  j++;
				  }
				  
			 	 $.ajax({
					url : "${ctp}/erpStorageFlow/erpStorageFlow_edit.do",
					type : "post",
					data : $("#ff").serialize(),
					beforeSend: function(XHR){
						$("#submitFBtn").attr("disabled",true); 
					},
					complete:function(XMLHttpRequest){
						$("#submitFBtn").attr("disabled",false); 
					},
					success : function(data) {
						var popBox = new top.Dialog({
							Width:300,
	       					Height:110
						});
						if (data.result) {
							popBox.Title = data.msg;
							popBox.InnerHtml = '<table height="100%" border="0" align="center" cellpadding="10" cellspacing="0">\
										<tr><td align="right"><input type="button" id="Icon_' + this.ID + '" class="icon_alert" align="absmiddle"></td>\
											<td align="left" id="Message_' + this.ID + '" style="font-size:' + (popBox.defaultFontSize) + 'px;font-family:' + (popBox.defaultFontFamily) + '">' + data.msg + '</td></tr>\
									</table>';
							popBox.ShowCloseButton = false;
							popBox.ShowCancelButton = false;
							
							popBox.OKEvent = function() {
								popBox.close();
								top.frmright.reflushList();
								top.Dialog.close();
							};
							popBox.show();
							popBox.okButton.parentNode.style.textAlign = "center";
						} else {
							top.Dialog.alert(data.msg, null, null, null, 2);
						};
					}
				});  
			 	
			 	
			 };
		}
		
		var g, goodsList;
		
		
		function initComplete(){
			
			/* $.ajax({
				url:"${ctp}/erpGoodInfo/companyAllGoods.do",
				success:function(data){
					var list = new Array();
					for(var i in data){
						list.push({"value": data[i].erpGoodInfoId, "key":data[i].goodName, "goodUnit":data[i].goodUnit, "suggest":data[i].goodCode +"|"+data[i].shortPinyin + "|"+data[i].fullPinyin, "cost":data[i].goodCostPrice});
					}
					goodsList = {"list":list};
					buildGrid();
				}
			}); */
			
			buildGrid();
		}
		
		function buildGrid(){
			var gdate = {"rows":[]};
			g = $("#maingrid").quiGrid({
				columns: [ 
				          { display: 'ID',  name: 'goodInfoId',  isAllowHide: true,   align: 'center', hide:true, width:"2%"},
				          { display: '商品',  name: 'goodName',  isAllowHide: false,   align: 'center',  width:"25%"
				        	 /*  editor:{type:'suggestion', autoCheck:true, data:goodsList, showList:true, inputWidth:200, tip:"请选择商品"}, render:function(item){
				        		  for (var i = 0; i < goodsList["list"].length; i++){
				        			  if (goodsList["list"][i]['value'] == item.goodName){
				        				  item.goodUnit = goodsList["list"][i]['goodUnit'];
				        				  item._cost = goodsList["list"][i]['cost'];
				        				  return goodsList["list"][i]['key'];
				        			  }
				        		  }
				        		  return item.goodName;
				        	  } */
				          },
				          { display: '单价',  name: 'cost', isAllowHide: false, align: 'center',  width:"20%", 
				        	  editor:{type:'text',  inputWidth:150, tip:"请输入数字"}
				          },
				          { display: '数量',  name: 'count', isAllowHide: false, align: 'center',  width:"20%", 
				        	   editor:{type:'text',  inputWidth:150, tip:"请输入数字"} 
				          },
				          { display: '单位',  name: 'goodUnit',  isAllowHide: false,   align: 'center',  width:"10%" },
				          { display: '操作', isAllowHide: false, align: 'center',  width:"15%", render: function (rowdata, rowindex, value, column){
							  return '<div class="padding_top4 padding_left5">'
							  + '<span class="img_delete hand" title="删除" onclick="onDel(\'' + rowindex+ '\')"></span>' 
							  + '</div>';
							}
						  }
				          ], 
				          data:gdate,
				          rownumbers:true,
				          percentWidthMode:false,
				          enabledSort:false,
				          checkbox:false,
				          usePager: false,
				          
				          enabledEdit: true,onBeforeEdit: onBeforeEdit, onBeforeSubmitEdit: onBeforeSubmitEdit,onAfterEdit: onAfterEdit,
				          
				          height: '86%',
				          width:"98%",
				          
				          frozen:false,
				          toolbar:{
				        	  items:[
				        	         ]
				          }
			});
		}
		
		/* function addColumn(){
			var totalRecord = g.records;
			var rowdata = {
					goodName: "",
					cost: "",
					count: 0,
					goodUnit: ""
		        };
			g.add(rowdata);
		} */
		
		
		function onBeforeEdit(e){
			//console.log('onBeforeEdit...');
		}
		
		function onBeforeSubmitEdit(e){
			//console.log('onBeforeSubmitEdit...');
		}
		
		function onAfterEdit(e){
			try{
				if(e.column.name == 'goodName'){
					e.record.goodUnit = e.record.goodUnit;
					e.record.cost = e.record._cost;
				}
				
				if(e.column.name == 'cost' || e.column.name == 'count'){
					countTotalPrice();
				}
				
				}catch (e1) {
					console.log(e1);
				}
			//选择商品后
		}
		
		function onDel(rowindex){
			var row = g.getRow(rowindex);
			g.deleteRow(row);
			countTotalPrice();
		}
		
		
		function countTotalPrice(){
			var totalPrice = 0;
			var gd = g.getData();
			for(var i in gd){
				if(gd[i].goodName == ''){
					continue;
					//$("#"+objId).tip({showCloseBtn:true,content: "请选择商品信息"});
				} else{
					totalPrice += parseInt(gd[i].cost) * parseInt(gd[i].count);
				}
			}
			$("#totalPrice").val(totalPrice);
			$("#show_totalPrice").html(totalPrice);
		}
		
		var goodsStock;
		$(function(){
			//选中仓库
			$("#sStorage").bind("change",function(){
		        if(!$(this).attr("relValue")){
		        }else{
		        	$.ajax({
						url:"${ctp}/erpStorage/erpStorage_stock.do?storageId="+$(this).attr("relValue"),
						success:function(data){
							goodsStock = data;
							var goods = new Array();
							for(var i in data){
								goods.push({"value":data[i].erpGoodInfo.erpGoodInfoId, "key":data[i].erpGoodInfo.goodName, "suggest":data[i].erpGoodInfo.goodName+"|"+data[i].erpGoodInfo.goodCode+"|"+data[i].erpGoodInfo.shortPinyin+"|"+data[i].erpGoodInfo.fullPinyin});
							}
							var suggestionData = {"list":goods};
							$("#suggestionGoods").data("data", suggestionData);
						    $("#suggestionGoods").render();
						}
		        	});
		        }
		    });
			
			//选中商品
			$("#suggestionGoods").bind("listSelect",function(e,obj){
				var id = obj.relValue;
				for(var i in goodsStock){
					if(goodsStock[i].erpGoodInfo.erpGoodInfoId == id){
						$("#addGoodsPrice").val(goodsStock[i].erpGoodInfo.goodSellPrice);
						$("#addGoodsCount").val("0");
						$("#addGoodsUnit").val(goodsStock[i].erpGoodInfo.goodUnit);
						$("#kucunSpan").html(goodsStock[i].goodCount +goodsStock[i].erpGoodInfo.goodUnit);
					}
				}
		    });
		    
			//添加商品
			$("#addGood").click(function(){
				//var id =$("#suggestionGoods").attr("relText");
				var id =$("#suggestionGoods").attr("relValue");
				if(id == undefined || id==''){
					top.Dialog.alert("请选择商品");
					return false;
				}
				var totalRecord = g.records;
				var rowdata = {
						goodInfoId:id,
						goodName: $("#suggestionGoods").attr("relText"),
						cost: $("#addGoodsPrice").val(),
						count: $("#addGoodsCount").val(),
						goodUnit: $("#addGoodsUnit").val()
			        };
				g.add(rowdata);
				countTotalPrice();
				$("#suggestionGoods").render();
				$("#addGoodsPrice").val("");
				$("#addGoodsCount").val("");
				$("#kucunSpan").html("");
			});

		});
		
	</script>
</body>
</html>

